Revising the Select Query I hackerrank solution in sql